package cst.a720yyq.zyfypt_720yyq;

import android.content.Context;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.view.menu.ActionMenuItemView;
import android.view.Menu;
import android.view.MenuItem;
import android.view.Window;
import android.webkit.WebView;
import android.widget.Toast;

import cst.a720yyq.zyfypt_720yyq.Bean.CollectResult;
import cst.a720yyq.zyfypt_720yyq.Interface.CollectListener;

import cst.a720yyq.zyfypt_720yyq.Interface.FocusListener;
import cst.a720yyq.zyfypt_720yyq.Modle.CollectModel;
import cst.a720yyq.zyfypt_720yyq.Modle.FocusModel;
import cst.a720yyq.zyfypt_720yyq.Modle.ResModle;

public class CaseActivity extends AppCompatActivity {

    private WebView webView;

    private String BASEURL="http://amicool.neusoft.edu.cn/";
    Context context;
    private Boolean flagcollect=false;//收藏标志
    private Boolean flagfocus=false;//关注标志

    private CollectModel collectmodel;//收藏model
    private FocusModel focusModel;
    private SharedPreferences sp;//简单存储
    private String sessionID="";  //sessionid
    private int userid;//资源用户id
    private int resid;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_artical);
        context=CaseActivity.this;
        System.out.println("----查看文章详情");
        resid  = getIntent().getIntExtra("id",1);
        userid = getIntent().getIntExtra("userid",7);//获取传递的资源用户id
        webView = (WebView)findViewById(R.id.art_web);
        webView.loadUrl(BASEURL+"tcase.php/show/index/id/"+resid);
        sp=context.getSharedPreferences("ME",MODE_PRIVATE);
        readSP();//读取sessionid

    }
    CollectListener listener=new CollectListener() {

        @Override
        public void onRecponce(String msg) {
            //获取菜单视图
            ActionMenuItemView item = (ActionMenuItemView) findViewById(R.id.menucollect);
            //根据mode中response返回的字符串区分返回结果
            switch (msg) {
                case "2":
                    System.out.println("----收藏成功");
                    flagcollect = true;
                    item.setTitle("取消收藏");
                    break;
                case "1":
                    System.out.println("----收藏失败");
                    break;
                case "5":
                    System.out.println("----取消收藏成功");
                    flagcollect = false;
                    item.setTitle("收藏");
                    break;
                case "4":
                    System.out.println("----取消收藏失败");
                    break;
                case "7":
                    System.out.println("----已收藏");
                    flagcollect = true;
                    item.setTitle("取消收藏");
                    break;
                case "8":
                    System.out.println("----未收藏");
                    flagcollect = false;
                    item.setTitle("收藏");
                    break;
                default:
                    Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
            }
        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
        }
    };

    FocusListener focusListener = new FocusListener() {
        @Override
        public void onResponse(String msg) {
            //获取菜单视图
            ActionMenuItemView item = (ActionMenuItemView) findViewById(R.id.menufocus);
            //根据mode中response返回的字符串区分返回结果
            switch (msg) {
                case "2":
                    System.out.println("----关注成功");
                    flagfocus = true;
                    item.setTitle("取消关注");
                    break;
                case "1":
                    System.out.println("----关注失败");
                    break;
                case "5":
                    System.out.println("----取消关注成功");
                    flagfocus = false;
                    item.setTitle("关注");
                    break;
                case "4":
                    System.out.println("----取消关注失败");
                    break;
                case "7":
                    System.out.println("----已关注");
                    flagfocus = true;
                    item.setTitle("取消关注");
                    break;
                case "8":
                    System.out.println("----未关注");
                    flagfocus = false;
                    item.setTitle("关注");
                    break;
                default:
                    Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
            }
        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
        }
    };


    private void readSP() {
        sessionID=sp.getString("session_id","");
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);//加载菜单布局
        collectmodel=new CollectModel();;//实例化对象
        collectmodel.exist("article",resid,sessionID,listener);//判断是否收藏
        focusModel = new FocusModel();
        focusModel.exist("userfocus",userid,sessionID,focusListener);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId())
        {
            case R.id.menucollect:
                //Toast.makeText(this, "文章收藏", Toast.LENGTH_SHORT).show();
                if(flagcollect)//如果已收藏，则调用取消收藏
                {
                    System.out.println("----准备取消收藏");
                    collectmodel.uncollect("article",resid,sessionID,listener);
                }
                else//如果未收藏，则调用收藏
                {
                    System.out.println("----准备收藏");
                    collectmodel.collect("article",resid,sessionID,listener);
                }
                break;
            case R.id.menufocus:
                //Toast.makeText(this, "文章关注", Toast.LENGTH_SHORT).show();
                if(flagfocus)//如果已关注，则调用取消关注
                {
                    System.out.println("----准备关注");
                    focusModel.unfocus("userfocus",userid,sessionID,focusListener);
                }
                else//如果未关注，则调用关注
                {
                    System.out.println("----准备取消关注");
                    focusModel.focus("userfocus",userid,sessionID,focusListener);
                }
                break;
        }
        return super.onOptionsItemSelected(item);
    }


}
